package viewControl;

import java.sql.SQLException;
import javax.swing.JOptionPane;
import model.DBConnector;
import model.DBHandler;
import model.JResult;

public class MainFrame extends javax.swing.JFrame {

    DBConnector dbc;
    DBHandler dbh;
    String settingsFileName;

    public MainFrame() {

        dbc = new DBConnector();
        settingsFileName = "settings";

        initComponents();
        optionPanel.setBounds(0, 0, 260, 330);
        optionPanel.setVisible(false);

        JResult ls = dbc.loadSettings(settingsFileName);
        if (ls.success) {
            JResult con = dbc.connect();
            if (con.success) {
                // Connected
                JOptionPane.showMessageDialog(null, con.message, "Info", 1);
            } else {
                // Couldn't connect!
                JOptionPane.showMessageDialog(null, con.message, "Error!", 0);
                optionPanel.setVisible(true);
                loginPanel.setVisible(false);
            }
        } else {
            // Couldn't Load Settings!
            JOptionPane.showMessageDialog(null, ls.message, "Error!", 0);
            optionPanel.setVisible(true);
            loginPanel.setVisible(false);
        }
        setLocationRelativeTo(null);
        dbh = new DBHandler(dbc);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        optionPanel = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtIPField = new javax.swing.JTextField();
        txtPortField = new javax.swing.JTextField();
        txtDBField = new javax.swing.JTextField();
        txtUserField = new javax.swing.JTextField();
        btnSaveSettings = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtPWField = new javax.swing.JPasswordField();
        loginPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        loginNavnTextField = new javax.swing.JTextField();
        passwordField = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        loginButton = new javax.swing.JButton();
        testComboBox = new javax.swing.JComboBox();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Login");
        setMinimumSize(new java.awt.Dimension(300, 350));
        getContentPane().setLayout(null);

        optionPanel.setMinimumSize(new java.awt.Dimension(320, 350));
        optionPanel.setPreferredSize(new java.awt.Dimension(320, 350));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Database Settings:");

        btnSaveSettings.setText("Save");
        btnSaveSettings.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveSettingsActionPerformed(evt);
            }
        });

        jLabel4.setText("IP / Hostname");

        jLabel5.setText("Port");

        jLabel6.setText("Database name");

        jLabel7.setText("Username");

        jLabel8.setText("Password");

        javax.swing.GroupLayout optionPanelLayout = new javax.swing.GroupLayout(optionPanel);
        optionPanel.setLayout(optionPanelLayout);
        optionPanelLayout.setHorizontalGroup(
            optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(optionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(optionPanelLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(188, 188, 188))
                    .addGroup(optionPanelLayout.createSequentialGroup()
                        .addGroup(optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnSaveSettings)
                            .addGroup(optionPanelLayout.createSequentialGroup()
                                .addGroup(optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel5)
                                    .addGroup(optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(optionPanelLayout.createSequentialGroup()
                                            .addGap(7, 7, 7)
                                            .addGroup(optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(jLabel7)
                                                .addComponent(jLabel8)
                                                .addComponent(jLabel4)))
                                        .addComponent(jLabel6)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(txtPWField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                                    .addComponent(txtIPField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                                    .addComponent(txtPortField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                                    .addComponent(txtUserField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                                    .addComponent(txtDBField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE))))
                        .addGap(91, 91, 91))))
        );
        optionPanelLayout.setVerticalGroup(
            optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(optionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addGroup(optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(optionPanelLayout.createSequentialGroup()
                        .addComponent(txtIPField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtPortField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addGap(18, 18, 18)
                        .addGroup(optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtDBField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtUserField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addGap(18, 18, 18)
                        .addGroup(optionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtPWField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSaveSettings)
                .addContainerGap(105, Short.MAX_VALUE))
        );

        getContentPane().add(optionPanel);
        optionPanel.setBounds(0, 0, 260, 270);

        loginPanel.setMinimumSize(new java.awt.Dimension(260, 310));

        jLabel1.setText("Bruger navn");
        jLabel1.setEnabled(false);

        loginNavnTextField.setEnabled(false);

        passwordField.setEnabled(false);

        jLabel2.setText("Password");
        jLabel2.setEnabled(false);

        loginButton.setText("Login");
        loginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginButtonActionPerformed(evt);
            }
        });

        testComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Admin", "Kunde", "Bud", "Køkken" }));

        jButton2.setText("Opret ny bruger");
        jButton2.setEnabled(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout loginPanelLayout = new javax.swing.GroupLayout(loginPanel);
        loginPanel.setLayout(loginPanelLayout);
        loginPanelLayout.setHorizontalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, loginPanelLayout.createSequentialGroup()
                        .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(testComboBox, 0, 111, Short.MAX_VALUE)
                            .addComponent(passwordField)
                            .addComponent(loginNavnTextField)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, loginPanelLayout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addGap(40, 40, 40)
                        .addComponent(loginButton)))
                .addContainerGap(42, Short.MAX_VALUE))
        );
        loginPanelLayout.setVerticalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginPanelLayout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addComponent(testComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginNavnTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(43, 43, 43)
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(loginButton))
                .addContainerGap(124, Short.MAX_VALUE))
        );

        getContentPane().add(loginPanel);
        loginPanel.setBounds(0, 0, 260, 270);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginButtonActionPerformed
    int brugernr = testComboBox.getSelectedIndex();
    try {
        switch (brugernr) {
            case 0:
                new GUI(brugernr, dbh).setVisible(true);
                break;
            case 1:
                new GUI(brugernr, dbh).setVisible(true);
                break;
            case 2:
                new GUI(brugernr, dbh).setVisible(true);
                break;
            case 3:
                new GUI(brugernr, dbh).setVisible(true);
                break;
        }
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, "Der er sket en med fejl databasen. Fejlkode: " + ex.getMessage(), "FEJL", 1);
    } catch (NullPointerException ex) {
        JOptionPane.showMessageDialog(null, "Der er sket en fejl med valget. Fejlkode: " + ex.getMessage(), "FEJL", 1);
    } catch (ArrayIndexOutOfBoundsException ex) {
        JOptionPane.showMessageDialog(null, "Fejlkode: " + ex.getMessage(), "FEJL", 1);
    }
}//GEN-LAST:event_loginButtonActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
}//GEN-LAST:event_jButton2ActionPerformed

private void btnSaveSettingsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveSettingsActionPerformed
    dbc.setAddress(txtIPField.getText());
    dbc.setPort(txtPortField.getText());
    dbc.setDBName(txtDBField.getText());
    dbc.setUName(txtUserField.getText());
    dbc.setPass(String.valueOf(txtPWField.getPassword()));
    JResult ss = dbc.saveSettings(settingsFileName);
    if (ss.success) {
        optionPanel.setVisible(false);
        loginPanel.setVisible(true);
        JOptionPane.showMessageDialog(null, ss.message, "Info", 1);
    } else {
        JOptionPane.showMessageDialog(null, ss.message, "Error!", 0);
    }
    dbh = new DBHandler(dbc);
}//GEN-LAST:event_btnSaveSettingsActionPerformed

    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    // <editor-fold desc="Variables declaration - do not modify ">
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSaveSettings;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JButton loginButton;
    private javax.swing.JTextField loginNavnTextField;
    private javax.swing.JPanel loginPanel;
    private javax.swing.JPanel optionPanel;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JComboBox testComboBox;
    private javax.swing.JTextField txtDBField;
    private javax.swing.JTextField txtIPField;
    private javax.swing.JPasswordField txtPWField;
    private javax.swing.JTextField txtPortField;
    private javax.swing.JTextField txtUserField;
    // End of variables declaration//GEN-END:variables
//</editor-fold>
}
